<template>
  <div class="box">
    <el-card style="background-color: #fff;width: 600px;padding: 10px 50px;
    margin: auto;  position: absolute;top: 0; left: 0; bottom: 0; right: 0;height: 350px" >
      <h1 style="text-align: center;margin: 30px auto">管理员登录</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名:" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input placeholder="请输入密码" v-model="ruleForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-if="this.loginButton==true" type="warning" @click="submitForm('ruleForm')">登录</el-button>
          <el-button v-if="this.loadingButton==true" :loading="true" type="warning" >正在登录,请稍后</el-button>
          <el-button type="warning"   @click="goWelcome()">返回首页</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>



  </div>
</template>

<script>
export default {
  data() {
    return {
      loginButton:true,
      loadingButton:false,
      ruleForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],

      }
    };
  },
  methods: {
    goWelcome(){
      this.$router.replace('/')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loginButton = false;
          this.loadingButton = true;
          let url="http://localhost:8080/staff/login";
          console.log('url='+url);
          let formData=this.qs.stringify(this.ruleForm);
          console.log('formData='+formData);
          this.axios.post(url,formData).then((response)=>{
            let responseBody=response.data;
            if (responseBody.state==20000){
              let jwt=responseBody.data;
              console.log('登录成功，jwt='+jwt);
              localStorage.setItem('jwt',jwt);
              localStorage.setItem('username',this.ruleForm.username);
              this.$router.push('/admin');
            }else {
              this.$message.error(responseBody.message);
              this.loginButton = true;
              this.loadingButton = false;
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>

  .box{
    position: relative;
    width: 100%;
    height: 900px;
    background: url("../assets/index/12.jpeg") center/100% 100% no-repeat;
    animation: carousel 15s infinite ;

  }
  @keyframes carousel {
    0%,20%{
      background: url("../assets/index/04.jpeg") center / 100% 100% no-repeat;
    }
    30%,50%{
      background: url("../assets/index/02.jpg") center / 100% 100% no-repeat;
    }
    60%,80%{
      background: url("../assets/index/07.jpeg") center / 100% 100% no-repeat;
    }
    90%,100%{
      background: url("../assets/index/08.jpeg") center / 100% 100% no-repeat;
    }
  }


</style>